package com.css.fxfzaqbz.modules.assess.repository.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.assess.entity.RiskAssessmentEntity;
import com.css.fxfzaqbz.modules.assess.repository.RiskAssessmentRepository;
import com.css.fxfzaqbz.modules.assess2.entity.TaskContent;
import com.css.fxfzaqbz.zcpt.sys.entity.SUser;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author y1fan
 * @create 2022-09-09 9:03
 */
@Repository
public class RiskAssessmentRepositoryImpl implements RiskAssessmentRepository {

    @Resource
    JdbcTemplate jdbcTemplate;
    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public RiskAssessmentEntity saveRiskAssessment(RiskAssessmentEntity riskAssessmentEntity) {
        String sql = "insert into sys_risk_assessment (sra_id,sra_ass_content,sra_ass_date,sra_ass_people,sra_ass_file_id,sra_ass_file_titel,sra_ass_file_describe,sra_conclusion,sra_sup_description,create_time,del_flag,create_user,sra_relation,types,mark,data_message) values(:sraId,:sraAssContent,:sraAssDate,:sraAssPeople,:sraAssFileId,:sraAssFileTitel,:sraAssFileDescribe,:sraConclusion,:sraSupDescription,:createTime,:delFlag,:createUser,:sraRelation,:types,:mark,:dataMessage)";

        SqlParameterSource source = new BeanPropertySqlParameterSource(riskAssessmentEntity);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
        return riskAssessmentEntity;
    }

    @Override
    public RiskAssessmentEntity save2RiskAssessment(RiskAssessmentEntity riskAssessmentEntity) {
        String sql = "update  sys_risk_assessment  set sra_ass_file_id =:sraAssFileId ,sra_ass_file_titel =:sraAssFileTitel, sra_ass_file_describe=:sraAssFileDescribe  where sra_id='"+riskAssessmentEntity.getSraId()+"'";

        SqlParameterSource source = new BeanPropertySqlParameterSource(riskAssessmentEntity);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
        return riskAssessmentEntity;
    }

    @Override
    public List<RiskAssessmentEntity> getAllRiskAssessment(String id) {
        String sql="";
        sql="select u.* from sys_risk_assessment u  where u.del_flag = '"+ YNEnum.N.toString() +"' and u.sra_relation='"+id+"' and mark='"+YNEnum.N+"'";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(RiskAssessmentEntity.class));
    }

    @Override
    public void deleteRiskAssessment(String id, String userID, Timestamp currentTimestamp) {
        String sql="";
        sql="update sys_risk_assessment  set del_flag ='"+YNEnum.Y.toString()+"' ,update_user='"+userID+"' ,update_time='"+currentTimestamp+"'  where sra_id = '"+id+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public RiskAssessmentEntity getRiskAssessmentById(String id) {
        String sql = "select * from sys_risk_assessment where sra_id = '"+id+"'  and del_flag = '"+YNEnum.N+"'";
        RiskAssessmentEntity riskAssessmentEntity = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(RiskAssessmentEntity.class));
        return riskAssessmentEntity;
    }

    @Override
    public RiskAssessmentEntity updateRiskAssessment(RiskAssessmentEntity riskAssessmentEntity) {
        String sql = "update  sys_risk_assessment  set sra_ass_content=:sraAssContent , sra_ass_people=:sraAssPeople,sra_ass_file_id =:sraAssFileId ,sra_ass_file_titel =:sraAssFileTitel, sra_ass_file_describe=:sraAssFileDescribe,sra_conclusion=:sraConclusion ,sra_sup_description=:sraSupDescription ,update_user =:updateUser,update_time=:updateTime where sra_id='"+riskAssessmentEntity.getSraId()+"'";

        SqlParameterSource source = new BeanPropertySqlParameterSource(riskAssessmentEntity);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
        return riskAssessmentEntity;
    }

    @Override
    public List<RiskAssessmentEntity>  getRiskAssessmentByContend(String sraAssContent,String relationId) {
        String sql="";
        sql="select u.* from sys_risk_assessment u  where  u.sra_ass_content='"+sraAssContent+"' and u.del_flag = '"+ YNEnum.N.toString() +"' and u.sra_relation ='"+relationId+"'";

        try {
            List<RiskAssessmentEntity> riskAssessmentEntities = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(RiskAssessmentEntity.class));
            return riskAssessmentEntities;
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public RiskAssessmentEntity updateRiskAssessmentById(RiskAssessmentEntity riskAssessmentEntity, String sraId) {
        String sql = "update  sys_risk_assessment  set sra_ass_content=:sraAssContent , sra_ass_people=:sraAssPeople,sra_ass_file_id =:sraAssFileId ,sra_ass_file_titel =:sraAssFileTitel, sra_ass_file_describe=:sraAssFileDescribe,sra_conclusion=:sraConclusion ,sra_sup_description=:sraSupDescription ,create_user =:createUser,create_time=:createTime ,update_time=:updateTime ,update_user=:updateUser where sra_id='"+sraId+"'";

        SqlParameterSource source = new BeanPropertySqlParameterSource(riskAssessmentEntity);
        //拿到新插入的主键值（i）
        KeyHolder keyHolder = new GeneratedKeyHolder();
        int i = namedParameterJdbcTemplate.update(sql, source, keyHolder);
        return riskAssessmentEntity;
    }

    @Override
    public List<TaskContent> getAllRiskAssessment1(String id) {
        String sql="";
        sql="select sra_id id,sra_ass_content as content,sra_ass_date as dateTime from sys_risk_assessment u  where u.del_flag = '"+ YNEnum.N.toString() +"' and u.sra_relation='"+id+"'";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(TaskContent.class));
    }

    @Override
    public List<RiskAssessmentEntity> getEarthEntityTwo(String id) {
        String sql="";
        sql="select u.* from sys_risk_assessment u  where u.del_flag = '"+ YNEnum.N.toString() +"' and u.sra_relation='"+id+"' and u.mark !='"+YNEnum.N+"' order by u.sra_ass_content";
        return jdbcTemplate.query(sql,new BeanPropertyRowMapper<>(RiskAssessmentEntity.class));

    }

    @Override
    public void firstSubmitRiskAssessment(String id, String userID, Timestamp currentTimestamp, String sraAssFileDescribe) {
        String sql="";
        sql="update sys_risk_assessment  set mark ='"+YNEnum.Y.toString()+"',update_user='"+userID+"',update_time='"+currentTimestamp+"',sra_ass_file_describe='"+sraAssFileDescribe+"' where sra_id = '"+id+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void firstSubmitRiskAssessment2(String id, String userID, Timestamp currentTimestamp) {
        String sql="";
        sql="update sys_risk_assessment  set mark ='"+YNEnum.Y.toString()+"',update_user='"+userID+"',update_time='"+currentTimestamp+"' where sra_id = '"+id+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public RiskAssessmentEntity riskAssessmentIfRepetition(RiskAssessmentEntity riskAssessmentEntity) {
        String sql="";
        sql="select u.* from sys_risk_assessment u  where u.del_flag = '"+ YNEnum.N.toString() +"' and u.sra_ass_file_titel='"+riskAssessmentEntity.getSraAssFileTitel()+"' and u.sra_relation='"+riskAssessmentEntity.getSraRelation()+"' and u.sra_ass_content ='"+riskAssessmentEntity.getSraAssContent()+"'";
        try {
            return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(RiskAssessmentEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public void addRiskAssessmentDisaster(List<String> ids, String describe) {
        String sql = "update sys_risk_assessment set sra_ass_file_describe = '" + describe + "' " +
                " where sra_id in (:ids)";
        Map map = new HashMap();
        map.put("ids", ids);
        namedParameterJdbcTemplate.update(sql, map);
    }

    @Override
    public void removeRiskAssessmentDisasterAss(RiskAssessmentEntity riskAssessmentEntity) {
        String sql="";
        sql="update sys_risk_assessment  set mark ='"+YNEnum.N.toString()+"',sra_ass_file_describe='"+riskAssessmentEntity.getSraAssFileDescribe()+"' ,submiter='"+riskAssessmentEntity.getSubmiter()+"' ,submit_phone='"+riskAssessmentEntity.getSubmitPhone()+"' where sra_id = '"+riskAssessmentEntity.getSraId()+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public void secondSubmitRiskAssessment(List<String> ids, SUser sUser,Timestamp currentTimestamp) {
        String sql = "update sys_risk_assessment set mark = '" + 2 + "' ,submiter='"+sUser.getUserName()+"',submit_phone='"+sUser.getMobilePhone()+"',update_time='"+currentTimestamp+"' ,submit_time='"+currentTimestamp+"'" +
                " where sra_id in (:ids)";
        Map map = new HashMap();
        map.put("ids", ids);
        namedParameterJdbcTemplate.update(sql, map);
    }

    @Override
    public RiskAssessmentEntity whatIsThisNumber(RiskAssessmentEntity riskAssessmentEntity) {
        String sql="";
        sql="select u.* from sys_risk_assessment u  where u.del_flag = '"+ YNEnum.N.toString() +"' and u.sra_ass_file_titel='"+riskAssessmentEntity.getSraAssFileTitel()+"' and u.sra_relation='"+riskAssessmentEntity.getSraRelation()+"'";
        try {
            return jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<>(RiskAssessmentEntity.class));
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public void deleteRiskAssessmentByTitel(String a, String userID, Timestamp currentTimestamp,String id) {
        String sql="";
        sql="update sys_risk_assessment  set del_flag ='"+YNEnum.Y.toString()+"' ,update_user='"+userID+"' ,update_time='"+currentTimestamp+"'  where sra_ass_file_titel = '"+a+"' and sra_relation='"+id+"'";
        jdbcTemplate.update(sql);
    }

    @Override
    public List<RiskAssessmentEntity> getRiskAssessmentByIaId(String id) {
        String sql = "select * from sys_risk_assessment where sra_relation='" + id + "' and del_flag='" + YNEnum.N + "' and mark='" + 2 + "'";
        List<RiskAssessmentEntity> riskAssessmentEntities = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(RiskAssessmentEntity.class));
        return riskAssessmentEntities;
    }

    @Override
    public List<RiskAssessmentEntity> getRiskByIaIdAndType(String iaId, String type) {
        String sql = "select * from sys_risk_assessment where sra_relation='" + iaId + "' and sra_ass_content='"+type+"' and del_flag='" + YNEnum.N + "' and mark='" + YNEnum.Y + "'";
        List<RiskAssessmentEntity> riskAssessmentEntities = null;
        try {
            riskAssessmentEntities = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(RiskAssessmentEntity.class));
            return riskAssessmentEntities;
        } catch (DataAccessException e) {
            return null;
        }

    }
}
